import { Modal, message } from 'antd';
import {
  FormPanel,
  FormPanelType,
  InputType,
  Utils,
  type FormPanelObject,
  FormPanelBtnAlign,
  FormPanelBtnDisplay,
} from 'lfun-bg-comp';
import React from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { getInstitutionCategory } from '@/utils';
import { InstitutionKeyInfo, createInstitution } from '@/service/unit/institution';
import useCountry from '@/hooks/useCountry';

const Groups: React.FC<{ show: boolean; onCancel: any; onOk: any }> = ({ show, onCancel, onOk }) => {
  const navigate = useNavigate();
  const location = useLocation();
  const country = useCountry();
  const createTemplate: FormPanelType[] = [
    {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
      span: 24,
      inputs: [
        {
          label: '单位名称',
          name: 'institution_name',
          key: 'institution_name',
          required: true,
          textRange: [0, 20],
        },
        // {
        //   label: '机构代码',
        //   name: 'institution_code',
        //   key: 'institution_code',
        //   required: true,
        //   textRange: [0, 50],
        // },
        {
          label: '地域管辖区',
          name: 'block',
          key: 'block',
          required: true,
          inputType: InputType.select,
          renderOption: country,
        },
        {
          label: '行政管辖区',
          name: 'borough',
          key: 'borough',
          required: true,
          inputType: InputType.select,
          renderOption: country,
        },
        {
          label: '隶属单位号',
          name: 'membership_no',
          key: 'membership_no',
          required: true,
        },
      ],
    },
  ];
  return (
    <Modal
      className="close_reson"
      open={show}
      title={`新增${location.pathname.indexOf('xz') !== -1 ? '行政' : '事业'}单位`}
      footer={[]}
      cancelText="返回"
      okText="确认"
      onCancel={onCancel}
    >
      <FormPanel
        btnsAlign={FormPanelBtnAlign.center}
        buttonsDisplay={FormPanelBtnDisplay.block}
        restBtnText="返回"
        submitBtnText="确定"
        values={{ planning_school_type: Utils.getProps('edu_type') }} // todo: 这里后续从token获取
        template={createTemplate}
        onFinish={async (formPanelObject: FormPanelObject) => {
          const values = formPanelObject.getFieldsValue();
          const planning: InstitutionKeyInfo = { ...values, institution_category: getInstitutionCategory() };
          const res = await createInstitution(planning);
          if (res.success) {
            message.success('新增成功');
            onCancel(formPanelObject);
            formPanelObject.resetFields();
            navigate(`${location.pathname}/change/${res.data.id}`, { state: { ...res.data } });
          } else {
            formPanelObject.reVaildation(res);
            return false;
          }
          return true;
        }}
        onReset={form => {
          if (typeof onCancel === 'function') {
            form.resetFields();
            onCancel(form);
          }
          return true;
        }}
      />
    </Modal>
  );
};

export default Groups;
